package com.stedu.security;

import cn.hutool.json.JSONUtil;
import com.stedu.bean.RespBean;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/*
* 认证
* 失败的处理器
* 验证码错误，密码用户名错误
* 认真失败如何处理
*
* */
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        RespBean error = RespBean.error(e.getMessage());

        PrintWriter writer = response.getWriter();
        writer.write(JSONUtil.toJsonStr(error));//转换json
        writer.flush();
        writer.close();

    }
}
